﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using VntNet.PowerSchemeSwitcher.Properties;

namespace VntNet.PowerSchemeSwitcher
{
    public partial class SettingsForm : Form
    {
        internal event Action SettingChanged;

        class PowerPlanComboBoxItem
        {
            PowerPlanInfo _info;

            internal Guid SchemeGuid
            {
                get { return _info.SchemeGuid; }
            }

            public PowerPlanComboBoxItem(PowerPlanInfo info)
            {
                _info = info;
            }

            public override string ToString()
            {
                return _info.FriendlyName;
            }
        }

        public SettingsForm()
        {
            InitializeComponent();
            var list=PowerSchemeHelper.GetAllPowerSchemas();
            foreach (var item in list)
            {
                acComboBox.Items.Add(new PowerPlanComboBoxItem(item.Value));
                batteryComboBox.Items.Add(new PowerPlanComboBoxItem(item.Value));
                lowBatteryComboBox.Items.Add(new PowerPlanComboBoxItem(item.Value));
            }

            SetComboSavedSettings(list, Settings.Default.OnAcScheme, acComboBox);
            SetComboSavedSettings(list, Settings.Default.OnFullBatteryScheme, batteryComboBox);
            SetComboSavedSettings(list, Settings.Default.OnLowBatteryScheme, lowBatteryComboBox);
        }

        private void SetComboSavedSettings(Dictionary<Guid, PowerPlanInfo> list, Guid settingGuid, ComboBox comboBox)
        {
            if (settingGuid != Guid.Empty)
            {
                if (list.ContainsKey(settingGuid))
                {
                    for (int i = 0; i < comboBox.Items.Count; i++)
                    {
                        if (((PowerPlanComboBoxItem)comboBox.Items[i]).SchemeGuid == settingGuid)
                        {
                            comboBox.SelectedIndex = i;
                            return;
                        }
                    }
                }
            }
        }

        void OnSettingChanged()
        {
            if (SettingChanged != null)
                SettingChanged();
        }
       
        private void ShowAbout()
        {
            AboutDialog dlg = new AboutDialog();
            dlg.ShowDialog();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            Settings.Default.Save();
            OnSettingChanged();
            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void SettingsForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            Settings.Default.Reload();
        }

        private void acComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            Settings.Default.OnAcScheme = ((PowerPlanComboBoxItem)acComboBox.SelectedItem).SchemeGuid;
        }

        private void batteryComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            Settings.Default.OnFullBatteryScheme = ((PowerPlanComboBoxItem)batteryComboBox.SelectedItem).SchemeGuid;
        }

        private void lowBatteryComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            Settings.Default.OnLowBatteryScheme = ((PowerPlanComboBoxItem)lowBatteryComboBox.SelectedItem).SchemeGuid;
        }

        private void pictureBox2_Click(object sender, EventArgs e)
        {
            ShowAbout();
        }

    }
}
